import { getName,getUserName } from '@/utils/auth';
//初始化data里面的数据（1、默认为空，2、之前存在的数据。。）
let itemDatas = {}
let itemListDatas= {};
export function datas(){
  itemDatas = {};
  itemListDatas=this.itemDataJson;
  this.itemList.forEach(val => {
    if(val.layout === 'rowItem'){ //row布局
      dataResolveRowItem(val);  //解析row布局
    }else if(val.layout === 'dynamicItem'){ //动态表单布局
      let obj =dataResolveDynamicItem(val);
      //let array = [];
      //array.push(obj);
      itemDatas[val.id] = obj;
    }else{    //表单
      dataResolveColItem(val);
    }
  })
  Object.keys(itemDatas).forEach(key =>{
    this.$set(this.form, key, itemDatas[key]);
  })
}

function dataResolveRowItem(val){
  const columns = val.columns;
  columns.forEach(v =>{
    v.list.forEach(item =>{
      if(item.layout==='dynamicItem'){
        let obj =dataResolveDynamicItem(item);
        //let array = [];
        //array.push(obj);
        itemDatas[item.id] = obj;
      }else{
        dataResolveColItem(item);
      }
    })
  })
}
function dataResolveDynamicItem(val){
  const columns = val.columns;
  let array = [];
  let obj ={};
  if(itemListDatas==undefined){
    columns.forEach(v =>{
      const key = v.id;
      obj[key] = v.value;
    });
    array.push(obj);
  }
  else{
    let tableData=itemListDatas[val.id];
    if(tableData!=undefined){
      if(tableData.length>0){
        const keys = Object.keys(tableData[0]);
        for(var i=0;i<tableData.length;i++){
          const table=tableData[i];
          obj ={};
          columns.forEach(v =>{
            const key = v.id;
            if(table[v.id]==undefined)
              obj[key] = v.value;
            else
              obj[key] = table[v.id];
          });
          array.push(obj);
        }
      }
      else{
        columns.forEach(v =>{
          const key = v.id;
          obj[key] = v.value;
        });
        array.push(obj);
      }
    }
    else{
      columns.forEach(v =>{
        const key = v.id;
        obj[key] = v.value;
      });
      array.push(obj);
    }

  }
  return array;
}

function dataResolveColItem(val){
  if(itemListDatas==undefined){
    itemDatas[val.id]=val.value;
  }
  else{
    //itemDatas[val.id] = val.value;
    if(val.value=="userInfo.name"){
      itemDatas[val.id]=getName();
    }
    else if(val.value=="userInfo.userId"){
      itemDatas[val.id]=getUserName();
    }
    else {
      if (val.value === "" || val.value === undefined || val.value === null|| val.value.length === 0) {
        if (val.compType == "select") {
          if (itemListDatas[val.id] === undefined){
            itemDatas[val.id] = val.value;
          }
          else{
            itemDatas[val.id] = itemListDatas[val.id];
          }
        }else if(val.compType=="inputNumber"){
          if (itemListDatas[val.id] === undefined){
            itemDatas[val.id] = val.value;
          }
          else{
            itemDatas[val.id] = itemListDatas[val.id];
          }
        }
        else {
          if (itemListDatas[val.id] === undefined){
            itemDatas[val.id] = val.value;
          }
          else{
            itemDatas[val.id] = itemListDatas[val.id];
          }
        }

      } else{
        if(val.compType=="inputNumber"){
          if (itemListDatas[val.id] === undefined){
            itemDatas[val.id] = val.value;
          }
          else{
            itemDatas[val.id] = itemListDatas[val.id];
          }
        }
        else{
          itemDatas[val.id] = val.value;
        }

      }

    }
  }
}

export function addRow(element){
  const columns = element.columns;
  let obj ={};
  columns.forEach(v =>{
    const key = v.id;
    obj[key] = v.value;
  })
  this.form[element.id].push(obj);
}
export function changTableData(arr,id){
  this.form[id].splice(0,this.form[id].length,...arr)
}

export function deleteRow(scope,element){
  this.form[element.id].splice(scope.$index,1);
}

export function batchDeleteRow(indexs,element){
  for(let i =0;i<indexs.length;i++){
    const index = indexs[i];
    this.form[element.id].splice(index,1);
  }
}
